from django.db import models
from django.contrib.auth.models import AbstractUser

class Role(models.Model):
    name=models.CharField('角色',max_length=32,unique=True)

    class Meta:
        db_table='tb_role'


class User(AbstractUser):
    # username=models.CharField(max_length=32)
    # password=models.CharField(max_length=255)
    email=models.CharField(max_length=255)
    phone=models.CharField(max_length=11)
    roles=models.ManyToManyField('Role',related_name='users')

    class Meta:
        db_table='tb_user'


class UrlInfo(models.Model):
    url=models.CharField('url',max_length=32,null=True)
    desc=models.CharField('描述',max_length=128,null=True)
    roles=models.ManyToManyField('Role',related_name='urlsinfo')

    class Meta:
        db_table='tb_urls'

    def __str__(self):
        return self.url


class UserRole(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    role = models.ForeignKey(Role, on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_userrole'

    def __str__(self):
        return '%s-%s'%(self.user.username,self.role.name)
